package com.fanhao.dllo.justforyou.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.adapter.JokePictureAdapter;
import com.fanhao.dllo.justforyou.entity.JokePictureList;
import com.fanhao.dllo.justforyou.entity.JokePictureResult;
import com.fanhao.dllo.justforyou.view.SwipeRefreshLoadingLayout;
import com.fanhao.dllo.justforyou.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by dllo on 15/10/23.
 */
public class JokePictureFragment extends Fragment implements SwipeRefreshLoadingLayout.OnRefreshListener, SwipeRefreshLoadingLayout.OnLoadListener {

    private RecyclerView recyclerView;
    private JokePictureAdapter jokePictureAdapter;

    VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private SwipeRefreshLoadingLayout swipeRefreshLoadingLayout;


    private Context context;
    private int position;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_joke_picture, null);
        return v;
    }

    /**
     * 初始化
     **/
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        swipeRefreshLoadingLayout = (SwipeRefreshLoadingLayout)view.
                findViewById(R.id.swipeRefreshLoadingLayout);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

          swipeRefreshLoadingLayout.setOnRefreshListener(this);
          swipeRefreshLoadingLayout.setOnLoadListener(this);


        initView();
//        jokePictureAdapter.setOnClickListener(this);
        initData();
    }

    public void initView() {

        //绑定布局管理器
        GridLayoutManager gm = new GridLayoutManager(getActivity(), 1);
        recyclerView.setLayoutManager(gm);

        //初始化及绑定适配器
        jokePictureAdapter = new JokePictureAdapter(getActivity());
        recyclerView.setAdapter(jokePictureAdapter);


    }

    public void initData() {


        String url = "http://api.budejie.com/api/api_open.php?market=taobao&maxid=&" +
                "udid=359357052150266&a=newlist&appname=baisibudejie&c=data&os=4.3&" +
                "client=android&userID=&page=1&per=20&sub_flag=1&visiting=&type=10&" +
                "time=week&mac=50%3AFC%3A9F%3AFC%3A75%3A7E&ver=6.0.1";

        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        swipeRefreshLoadingLayout.setRefreshing(false);
                        Gson gson = new Gson();
                        JokePictureResult jpr = gson.fromJson(response,
                                JokePictureResult.class);
                        if (jpr != null) {
                            List<JokePictureList> list = jpr.getList();
                            if (list != null && list.size() > 0) {
                                jokePictureAdapter.addData(list);
                            }
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };

        volleySingleton.addRequest(request);
    }

    @Override
    public void onRefresh() {
        initData();

    }

    @Override
    public void onLoad() {
        loadData();

    }

    private void loadData() {
        String url = "http://api.budejie.com/api/api_open.php?market=taobao&maxid=&" +
                "udid=359357052150266&a=newlist&appname=baisibudejie&c=data&os=4.3&" +
                "client=android&userID=&page=1&per=20&sub_flag=1&visiting=&type=10&" +
                "time=week&mac=50%3AFC%3A9F%3AFC%3A75%3A7E&ver=6.0.1";

        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        swipeRefreshLoadingLayout.setLoading(false);
                        Gson gson = new Gson();
                        JokePictureResult jpr = gson.fromJson(response,
                                JokePictureResult.class);
                        if (jpr != null) {
                            List<JokePictureList> list = jpr.getList();
                            if (list != null && list.size() > 0) {
                                jokePictureAdapter.addData(list);
                            }
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };

        volleySingleton.addRequest(request);
    }


}


//    @Override
//    public void onItemClick(JokePictureList j) {
//        Intent intent = new Intent(getActivity(), JokePictureCommentActivity.class);
//        startActivity(intent);
//    }

